package com.tv.nextin;

import com.tv.nextin.Utils.Common;
import com.tv.nextin.Utils.KeyboardHelper;
import com.tv.nextin.bean.Person;
import com.tv.nextin.bean.Staff;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class BookingActivity extends Activity implements OnTouchListener{

	TextView title, txtNameStaff;
	ImageButton btnBack;
	
	ViewGroup main;
	
	EditText edtName, edtEmail, edtPhone;
	
	Button btnBooking;
	private BroadcastReceiver mReceiver;
	
	Staff staff = new Staff();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_booking);
		
		title = (TextView) findViewById(R.id.title);
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnBack.setVisibility(View.VISIBLE);
		main = (ViewGroup) findViewById(R.id.main);
		KeyboardHelper.setupUI(BookingActivity.this, main);
		
		staff = getIntent().getParcelableExtra("staff");
		
		txtNameStaff = (TextView) findViewById(R.id.txt_name_staff);

		edtName = (EditText) findViewById(R.id.edt_name);
		edtEmail = (EditText) findViewById(R.id.edt_email);
		edtPhone = (EditText) findViewById(R.id.edt_mobile);
		
		btnBooking = (Button) findViewById(R.id.btn_booking);
		
		init();
		actionImei();
		receiverBroastCast();
	}
	
	void actionImei(){
		edtName.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_NEXT){
					edtEmail.requestFocus();
					return true;
				}
				
				return false;
			}
		});
		
		edtEmail.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_NEXT){
					edtPhone.requestFocus();
					return true;
				}
				
				return false;
			}
		});
		
		edtPhone.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_NEXT){
					booking();
					return true;
				}
				return false;
			}
		});
		
	}
	
	
	void booking(){
		if (!edtName.getText().toString().equals("") && (!edtEmail.getText().toString().equals("") && (!edtPhone.getText().toString().equals("")))){
			Person person = new Person();
			person.setName(edtName.getText().toString());
			person.setEmail(edtEmail.getText().toString());
			person.setPhone(edtPhone.getText().toString());
			
			Intent t = new Intent(BookingActivity.this, VerifyCodeActivity.class);
			t.putExtra("person", person);
			startActivity(t);
			overridePendingTransition(R.anim.trans_left_in, R.anim.over_main_left);
			
		} else {
			Toast.makeText(BookingActivity.this, "Field isn't blank!", Toast.LENGTH_SHORT).show();
		}
		
	}
	
	void init(){
		title.setText("Booking");
		btnBack.setOnTouchListener(this);
		btnBooking.setOnTouchListener(this);
		txtNameStaff.setText(staff.getName());
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.btn_back:
			if (event.getAction() == MotionEvent.ACTION_UP){
				closeActivity();
			}
			break;
		case R.id.btn_booking:
			if (event.getAction() == MotionEvent.ACTION_UP){
				booking();
			}
			break;
		default:
			break;
		}
		return false;
	}
	
	void receiverBroastCast(){
		IntentFilter intentFilter = new IntentFilter();
		
		
		 intentFilter.addAction(Common.FINISH);
		    
		    mReceiver = new BroadcastReceiver() {
				
				@Override
				public void onReceive(Context context, final Intent intent) {
					String action = intent.getAction();
					if (action.equals(Common.FINISH)){
						finish();
					} 
				}
			};
			// register
			this.registerReceiver(mReceiver, intentFilter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.unregisterReceiver(mReceiver);
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.over_main_right, R.anim.trans_right_out);
	}
	
	
	@Override
	public void onBackPressed() {
		closeActivity();
	}
	
	
}
